package com.chat.db;

import java.util.Hashtable;

/**
 * 管理所有的数据配置对象
 */
public class ConfigurationPool {

    private static volatile ConfigurationPool instance;
    private static volatile Hashtable<String, Configuration> configs;
    private ConfigurationPool() {
        configs = new Hashtable<>();
    }

    public static ConfigurationPool getInstance() {

        if (instance == null) {
            synchronized (ConfigurationPool.class) {
                if (instance == null) instance = new ConfigurationPool();
            }
        }
        return instance;
    }

    /**
     * 添加新的配置
     * @param key 配置唯一键
     * @param config 配置
     * @return 配置
     */
    public Configuration put(String key, Configuration config) {
        if (isExist(key)) return get(key);

        configs.put(key, config);
        return config;
    }

    /**
     * 获取配置
     * @param key 配置唯一键
     * @return 配置
     */
    private Configuration get(String key) {
        return configs.get(key);
    }

    /**
     * 检测配置是否存在
     * @param key 配置唯一键
     * @return 是否存在
     */
    public boolean isExist(String key) {
        return configs.containsKey(key);
    }
}
